/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.verify.constraint;

import picounit.verify.constraint.Constraint;

public class ContainsElement extends Constraint<Object[], ContainsElement> {
	private final Object element;
	private ArrayModifier modifier;
	private final ArrayUtil arrayUtil = new ArrayUtil();

	public ContainsElement(Object element, ArrayModifier modifier) {
		this.element = element;
		this.modifier = modifier;
	}

	@Override
	public boolean evaluate(Object[] searchIn) {
		return searchIn != null && arrayUtil.contains(modify(searchIn), modify(element));
	}

	@Override
	public String describeFailure() {
		return "does not contain <" + element + "> " + modifier.getName();
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	private Object[] modify(Object[] values) {
		return (Object[]) modifier.modify(values);
	}

	private Object modify(Object value) {
		return modifier.modifyElement(value);
	}
}
